package cn.mdmm.service.impl.weixin;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.weixin.config.WxConfig;
import com.weixin.core.TokenFactory;
import com.weixin.core.WxHandleFactory;
import com.weixin.utils.WxWeb;

import cn.mdmm.common.MemchWeixinToken;
import cn.mdmm.common.WeiXinMsgHandle;
import cn.mdmm.common.WeiXinOrderProcess;
import cn.mdmm.service.weixin.WeiXinServer;
import cn.modoumama.common.utils.ConfigProperty;

/**
 * 类描述：微信消息处理<br>
 * 创建人：邓强 <br>
 * 创建时间：2017年7月31日 上午10:30:16 <br>
 * 修改人： <br>
 * 修改时间：2017年7月31日 上午10:30:16 <br>
 * 修改备注： <br>
 * 
 * @version V1.0
 */
@Service("weiXinServer")
public class WeiXinServerImpl implements WeiXinServer {
	Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	MemchWeixinToken memchWeixinToken;
	@Autowired
	WeiXinMsgHandle weiXinMsgHandle;
	@Autowired
	WeiXinOrderProcess weiXinOrderProcess;

	@Override
	public Boolean validateWx(String signature, String timestamp, String nonce) {
		logger.info("signature=" + signature + ",timestamp=" + timestamp + ",nonce=" + nonce);
		boolean flag = false;
		if (signature == null || timestamp == null || nonce == null) {
			logger.warn("微信服务器验证服务器地址的有效性_参数异常");
		}else{
			flag = WxWeb.check(signature, timestamp, nonce);
		}
		
		return flag;
	}
	
	@PostConstruct
	public void init() {
		WxConfig.token = ConfigProperty.getProperty("weixin.server.token");
		TokenFactory.initialWeixinToken(memchWeixinToken);
		WxHandleFactory.initial(weiXinMsgHandle, weiXinOrderProcess);
	}

	@Override
	public String handleMsg(String xml) {
		return WxHandleFactory.Handle(xml);
	}

	@Override
	public String wxOrder(String xml) {
		return WxHandleFactory.processOrder(xml);
	}

	// ---------------新版接收消息新版 结束-------------------
}
